package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/wechatrpc/internal/svc"
	"wechat/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *wechatrpc.LoginRequest) (*wechatrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var wechat mysql.Wechat
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&wechat)
	if wechat.ID == 0 {
		return nil, fmt.Errorf("当前账号未登录，请前去注册")
	}
	if wechat.Password != in.Password {
		return nil, fmt.Errorf("当前密码错误，请重新输入")
	}
	return &wechatrpc.LoginResponse{
		WechatId: int64(wechat.ID),
	}, nil
}
